<?php
$echoStr = $_GET["echostr"];   
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];           
$token = 'shijian1234';
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//如果用户是验证的时候会存在$_GET['echostr'] 
if( $tmpStr == $signature && isset($_GET['echostr'])){
    echo $echoStr;
    exit;
}else{//如果用户不是验证（比如订阅或者回复消息）
	//1.接收微信服务器推送过来的消息(xml格式,字符串类型)
	$wxXML = $GLOBALS['HTTP_RAW_POST_DATA'];
	//2.处理消息类型
	$wxObj = simplexml_load_string($wxXML);
	
	//发送方
	$FromUserName = $wxObj->ToUserName;
	//接收方
	$ToUserName = $wxObj->FromUserName;
	$CreateTime = time();
	$MsgType = 'text';
	
	//如果是事件类型event
	if($wxObj->MsgType == 'event'){
		//如果是订阅
		if($wxObj->Event == 'subscribe'){
			$Content = '敢关注我？你已被定位，迅速去揍你！😄';
			//给用户回复默认欢迎消息
			$template = <<<str
				<xml>
				<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
				<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
				<CreateTime>{$CreateTime}</CreateTime>
				<MsgType><![CDATA[{$MsgType}]]></MsgType>
				<Content><![CDATA[{$Content}]]></Content>
				</xml>
str;
			echo $template;exit;
			
		}
	}
	
	//如果用户给我们发送消息，那么类型为text
	if($wxObj->MsgType == 'text'){
		switch ($wxObj->Content) {
			case '1':
				$Content = '1是美女，送你一个';
				//给用户回复默认欢迎消息
				$template = <<<str
					<xml>
					<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
					<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
					<CreateTime>{$CreateTime}</CreateTime>
					<MsgType><![CDATA[{$MsgType}]]></MsgType>
					<Content><![CDATA[{$Content}]]></Content>
					</xml>
str;
				echo $template;exit;
				break;
			case '2':
				$Content = '2是帅哥，送你一个';
				//给用户回复默认欢迎消息
				$template = <<<str
					<xml>
					<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
					<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
					<CreateTime>{$CreateTime}</CreateTime>
					<MsgType><![CDATA[{$MsgType}]]></MsgType>
					<Content><![CDATA[{$Content}]]></Content>
					</xml>
str;
				echo $template;exit;
				break;
			case '你好':
				$Content = '你好你好，大家都好';
				//给用户回复默认欢迎消息
				$template = <<<str
					<xml>
					<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
					<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
					<CreateTime>{$CreateTime}</CreateTime>
					<MsgType><![CDATA[{$MsgType}]]></MsgType>
					<Content><![CDATA[{$Content}]]></Content>
					</xml>
str;
				echo $template;exit;
				break;	
			case '3':
				$data = array(
					array(
						'title'=>'c59期',
						'description'=>'将要毕业的59期',
						//图片地址
						'picurl' => 'http://www.hdmzy.com/wx/c59.jpg',
						//跳转地址
						'url' => 'http://www.hdmzy.com/wx/c59.html',
					),
					array(
						'title'=>'c59期',
						'description'=>'将要毕业的59期',
						//图片地址
						'picurl' => 'http://www.hdmzy.com/wx/c59.jpg',
						//跳转地址
						'url' => 'http://www.houdunwang.com',
					),
					array(
						'title'=>'c59期',
						'description'=>'将要毕业的59期',
						//图片地址
						'picurl' => 'http://www.hdmzy.com/wx/c59.jpg',
						//跳转地址
						'url' => 'http://www.houdunwang.com',
					),
				);
				$template = "<xml>
				<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
				<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
				<CreateTime>{$CreateTime}</CreateTime>
				<MsgType><![CDATA[news]]></MsgType>
				<ArticleCount>".count($data)."</ArticleCount>
				<Articles>";
				foreach ($data as $v) {
					$template .= "<item>
							<Title><![CDATA[{$v['title']}]]></Title> 
							<Description><![CDATA[{$v['description']}]]></Description>
							<PicUrl><![CDATA[{$v['picurl']}]]></PicUrl>
							<Url><![CDATA[{$v['url']}]]></Url>
							</item>";
				}
				$template .= "</Articles></xml>";
				echo $template;exit;
				break;
			default:
				$Content = '不知道你说啥，整理下语言吧，笨蛋！';
				//给用户回复默认欢迎消息
				$template = <<<str
					<xml>
					<ToUserName><![CDATA[{$ToUserName}]]></ToUserName>
					<FromUserName><![CDATA[{$FromUserName}]]></FromUserName>
					<CreateTime>{$CreateTime}</CreateTime>
					<MsgType><![CDATA[{$MsgType}]]></MsgType>
					<Content><![CDATA[{$Content}]]></Content>
					</xml>
str;
				echo $template;exit;
				break;
		}
	}
	
	
}












?>